package com.saint.easemobtest;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.hyphenate.EMCallBack;
import com.hyphenate.EMMessageListener;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMessage;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.List;

public class TestChatActivity extends AppCompatActivity {

    private String userId;
    private EMMessage emMessage;
    private StringBuilder sb;
    private EMMessageListener emMessageListener;

    @ViewInject(R.id.all_msg_tv)
    private TextView allMsg_tv;
    @ViewInject(R.id.msg_et)
    private EditText msg_et;

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1://收到消息
                    List<EMMessage> list = (List) msg.obj;

                    for (EMMessage message : list){
                        if (message.getFrom().equals(userId)){
                            setReceivedMsgToView(message);
                        }else {

                        }
                    }

                    break;
                case 2://


                    break;

                default:
                    break;
            }
        }
    };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);
        x.view().inject(this);

        userId = getIntent().getStringExtra("ec_chat_id");
        initMsgListener();

    }

    @Override
    protected void onResume() {
        super.onResume();
        EMClient.getInstance().chatManager().addMessageListener(emMessageListener);
    }

    @Override
    protected void onStop() {
        super.onStop();
        EMClient.getInstance().chatManager().removeMessageListener(emMessageListener);
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.send_msg_btn:
                sendTextMsg();
                break;

            default:
                break;

        }
    }

    private void sendTextMsg(){
        if (TextUtils.isEmpty(userId)){
            Toast.makeText(this, "好友Id为空，当前不能发送信息", Toast.LENGTH_SHORT).show();
            return;
        }

        final String contentMsg = msg_et.getText().toString().trim();
        if (TextUtils.isEmpty(contentMsg)){
            Toast.makeText(this, "发送消息不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        emMessage = EMMessage.createTxtSendMessage(contentMsg, userId);
        setSendMsgIntoView(emMessage);

        EMClient.getInstance().chatManager().sendMessage(emMessage);
        emMessage.setMessageStatusCallback(new EMCallBack() {
            @Override
            public void onSuccess() {
                Toast.makeText(TestChatActivity.this, "消息发送成功", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError(int code, String error) {
                Toast.makeText(TestChatActivity.this, "消息发送失败： code: " + code + "  error: " + error, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onProgress(int progress, String status) {

            }
        });
    }

    private void setSendMsgIntoView(EMMessage  msg){
        if (TextUtils.isEmpty(allMsg_tv.getText().toString().trim())){
            allMsg_tv.setText("发送：" + msg + " - time: " + msg.getMsgTime());
        }else {
            sb = new StringBuilder(allMsg_tv.getText().toString().trim());
            sb.append("\n" + "发送：" + msg + " - time: " + msg.getMsgTime());
            allMsg_tv.setText(sb.toString());
        }
    }

    private void setReceivedMsgToView(EMMessage msg){
        if (TextUtils.isEmpty(allMsg_tv.getText().toString().trim())){
            allMsg_tv.setText("接收：" + msg + " - time: " + msg.getMsgTime());
        }else {
            sb = new StringBuilder(allMsg_tv.getText().toString().trim());
            sb.append("\n" + "接收：" + msg + " - time: " + msg.getMsgTime());
            allMsg_tv.setText(sb.toString());
        }
    }

    private void initMsgListener(){
        emMessageListener = new EMMessageListener() {
            @Override
            public void onMessageReceived(List<EMMessage> messages) {
                Message msg = handler.obtainMessage();
                msg.what = 1;
                msg.obj = messages;
                handler.sendMessage(msg);
            }

            @Override
            public void onCmdMessageReceived(List<EMMessage> messages) {

            }

            @Override
            public void onMessageRead(List<EMMessage> messages) {

            }

            @Override
            public void onMessageDelivered(List<EMMessage> messages) {

            }

            @Override
            public void onMessageChanged(EMMessage message, Object change) {

            }
        };
    }



}
